package lastfm
{
  import asunit.framework.TestCase;
  import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.net.URLVariables;
  import lastfm.events.RequestEvent;

  /**
   * ...
   * @author Roman Marchenko
   */
  public class TestRequest extends TestCase
  {

    public function TestRequest(testMethod:String = null)
    {
      super(testMethod);
    }

    public function testWrongRequest (): void
    {
      var request: Request = new Request;
      var params: URLVariables = new URLVariables;
      params["method"] = "xxx";

      var handler: Function = function (event: RequestEvent): void
      {
        assertNotNull(event.xml);
      };

      request.addEventListener(RequestEvent.ERROR, addAsync(handler, 10000));
      request.send(params);
    }

    public function testNullRequest (): void
    {
      var request: Request = new Request;
      var params: URLVariables = new URLVariables;
      
      var handler: Function = function (event: RequestEvent): void
      {
        assertNotNull(event.xml);
      };

      request.addEventListener(RequestEvent.ERROR, addAsync(handler, 10000));
      request.send(params);
    }

    public function testWrongRequestParameter (): void
    {
      var request: Request = new Request;
      var params: URLVariables = new URLVariables;
      params["method"] = "artist.getcorrection";
      params["name"] = "Guns and Roses";

      var handler: Function = function (event: RequestEvent): void
      {
        assertNotNull(event.xml);
      };
      
      request.addEventListener(RequestEvent.ERROR, addAsync(handler, 10000));
      request.send(params);    
    }

    public function testCorrectRequest (): void
    {
      var request: Request = new Request;
      var params: URLVariables = new URLVariables;
      params["method"] = "artist.getcorrection";
      params["artist"] = "Guns and Roses";

      var handler: Function = function (event: RequestEvent): void
      {
        assertNotNull(event.xml);
      };
      
      request.addEventListener(RequestEvent.OK, addAsync(handler, 10000));
      request.send(params);    
    }

    public function testEncodedNameRequest (): void
    {
      var request: Request = new Request;
      var params: URLVariables = new URLVariables;
      params["method"] = "artist.gettoptracks";
      params["artist"] = "Florence + The Machine";
      params["limit"] = 1;
      params["page"] = 1;

      var handler: Function = function (event: RequestEvent): void
      {
        assertNotNull(event.xml);
      };
      
      request.addEventListener(RequestEvent.OK, addAsync(handler, 10000));
      request.send(params);    
    }
  }

}